package fatworm.query.expression;

import fatworm.query.Plan;
import fatworm.query.Scan;
import fatworm.storage.table.*;

public class MultiTerms implements Term {
	
	private Term lterm, rterm;
	private int opcode;
	
	public MultiTerms(Term lterm, Term rterm, int opcode) {
		this.lterm = lterm;
		this.rterm = rterm;
		this.opcode = opcode;
	}
	
	public boolean isOK(Scan s) {
		if (opcode == 0)
			return lterm.isOK(s) || rterm.isOK(s);
		else
			return lterm.isOK(s) && rterm.isOK(s);
	}
	
	public boolean isIn(Schema sch) {
		return lterm.isIn(sch) && rterm.isIn(sch);
	}
	
	public int reductionFactor(Plan p) {
		
		float l = lterm.reductionFactor(p);
		float r = rterm.reductionFactor(p);
		
		if (opcode == 0)
			return Math.round(1 / ( 1 / l + 1 / r - 1 / r / l));   
		else
			return Math.round(l * r);
	}
	
	public String toString() {
		String op = "";
		
		if (opcode == 0)
			op = " or ";
		else
			op = " and ";
		
		return lterm.toString() + op + rterm.toString();
	}
}